using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace DAL
{
    public class tbSeriDAL
    {
        private SqlConnection cn;
        public tbSeriDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public tbSeriDAL(tbSeriDAL seRi)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public void Insert(tbSeri seRi)
        {
            SqlCommand cm = new SqlCommand("tbSeri_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@sanpham", seRi.sanpham);
            cm.Parameters.AddWithValue("@seri", seRi.seri);
            cm.Parameters.AddWithValue("@damua", seRi.damua);
            cm.ExecuteNonQuery();
        }

        public void Update(tbSeri seRi)
        {
            SqlCommand cm = new SqlCommand("tbSeri_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@sott", seRi.sott);
            cm.Parameters.AddWithValue("@sanpham", seRi.sanpham);
            cm.Parameters.AddWithValue("@seri", seRi.seri);
            cm.Parameters.AddWithValue("@damua", seRi.damua);
            cm.ExecuteNonQuery();
        }

        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tbSeri_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@sott", iD);
            cm.ExecuteNonQuery();
        }

        public tbSeriCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tbSeri_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbSeriCollection srCollection = new tbSeriCollection();

            while (dr.Read())
            {
                tbSeri seRi = new tbSeri();
                seRi.sott  = int.Parse(dr.GetValue(0).ToString());
                seRi.sanpham  = int.Parse(dr.GetValue(1).ToString());
                seRi.seri  = dr.GetValue(2).ToString();
                seRi.damua = int.Parse(dr.GetValue(3).ToString());
                srCollection.Add(seRi);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return srCollection;
        } 
    }
}
